package com.wei.shopping.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wei.shopping.dao.SoldMapper;
import com.wei.shopping.entrty.PageResult;
import com.wei.shopping.entrty.QueryPageBean;
import com.wei.shopping.pojo.Commodity;
import com.wei.shopping.pojo.Sold;
import com.wei.shopping.service.SoldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SoldServiceImpl implements SoldService {
    @Resource
    private SoldMapper soldMapper;
    /**
     * 分页查找
     * @return
     */
    @Override
    public PageResult findall(QueryPageBean queryPageBean, String findString){
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Sold> page = soldMapper.findall(queryPageBean.getQueryString(),findString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 按性别分组查询每个月的购买人群
     */
    public List<Integer> findGroupBySex(List<String> months, String sex){
        List<Integer> sexlist = new ArrayList<>();
        for (String month : months) {
            String begin = month + "-0";
            String end = month + "-31";
            Integer number = soldMapper.findGroupBySex(begin,end,sex);
            sexlist.add(number);
        }
        return sexlist;
    }

    /**
     * 查找已售商品
     * @param userid
     * @return
     */
    @Override
    public PageResult findallbyuserid(String userid,QueryPageBean queryPageBean){
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Sold> pages = soldMapper.findallbyuserid(userid, queryPageBean.getQueryString());
        return new PageResult(pages.getTotal(),pages.getResult());
    }

    /**
     * 删除销售记录
     * @param soldid
     */
    @Override
    public void deletecomm(String soldid){
        soldMapper.deletecomm(soldid);
    }
    /**
     * 根据userid查找每个月的销售数量
     * @param months
     * @param userid
     * @return
     */
    @Override
    public List<Integer> findAccount(List<String> months,String userid){
        List<Integer> countlist = new ArrayList<>();
        for (String month : months) {
            String begin = month + "-0";
            String end = month + "-31";
            Integer number = soldMapper.findAccount(begin,end,userid);
            countlist.add(number);
        }
        return countlist;
    }
    /**
     * 通过commid查找销售记录
     * @param commid
     * @return
     */
    @Override
    public Sold findByCommid(String commid){
        return soldMapper.findByCommid(commid);
    }

    /**
     * 添加销售记录
     * @param sold
     */
    @Override
    public void addSold(Sold sold){
        soldMapper.addSold(sold);
    }

    /**
     * 查找我的订单
     * @param queryPageBean
     * @param userid
     * @return
     */
    @Override
    public PageResult findorder(QueryPageBean queryPageBean,String userid){
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Sold> pages = soldMapper.findorder(queryPageBean.getQueryString(),userid);
        return new PageResult(pages.getTotal(),pages.getResult());
    }
}
